(function($) {
    $(function(event) {

    	$('#setContract').click(function() {
    		var elt = $(this);
    		if (elt.attr('disabled') == 'disabled') {
    			return;
    		}
    		elt.attr('disabled', 'disabled');

    		var userId = $('#userId').val();
    		var status = '0';
    		if ($(this).hasClass('valid-error')) {
    			status = '1';
    		}

			var result = $.ajax({
				url: '/a_admin/user.changeContract?id=' + userId + '&setContract=' + status,
				type: "GET",
				timeout: 5000,
				/*dataType : "json",*/
				success: function(data) {
					if (data > 0) {
						if (status == '1') {
							elt
							.removeClass('valid-error')
							.addClass('valid-ok')
							.attr('title', 'CONTRACT_GET_TRUE'.translate());
						} else {
							elt
							.removeClass('valid-ok')
							.addClass('valid-error')
							.attr('title', 'CONTRACT_GET_FALSE'.translate());
						}
						$('#contractError').hide();
						$('#contractSuccess').html('CONTRACT_GET_UPDATE_SUCCESS'.translate()).show();
					} else {
						$('#contractSuccess').hide();
						$('#contractError').html('CONTRACT_GET_UPDATE_ERROR'.translate()).show();
					}
				},
				error: function() {
					$('#contractSuccess').hide();
					$('#contractError').html('CONTRACT_GET_UPDATE_ERROR'.translate()).show();
				}
			});

			elt.removeAttr('disabled');
    	});

    	$('#setOffers').click(function() {
    		var elt = $(this);
    		if (elt.attr('disabled') == 'disabled') {
    			return;
    		}
    		elt.attr('disabled', 'disabled');

    		var userId = $('#userId').val();
    		var status = '0';
    		if (this.checked) {
    			status = '1';
    		}

    		var result = $.ajax({
    			url: '/a_admin/user.changeOffers?id=' + userId + '&setOffers=' + status,
    			type: "GET",
    			timeout: 5000,
    			/*dataType : "json",*/
    			success: function(data) {
					if (data > 0) {
						$('#offersError').hide();
						$('#offersSuccess').html('OFFERS_GET_UPDATE_SUCCESS'.translate()).show();
					} else {
						$('#offersSuccess').hide();
						$('#offersError').html('OFFERS_GET_UPDATE_ERROR'.translate()).show();
					}
	    		},
	    		error: function() {
	    			$('#offersSuccess').hide();
	    			$('#offersError').html('OFFERS_GET_UPDATE_ERROR'.translate()).show();
	    		}
    		});

    		elt.removeAttr('disabled');
    	});
    });
})(jQuery);